import calendar from 'js-calendar-converter';

export const formatTimestamp = (timestamp) => {
	if (String(timestamp).length === 10) {
		timestamp = timestamp * 1000
	}
	const date = new Date(timestamp);
	const now = new Date();
	const year = date.getFullYear();
	const month = date.getMonth() + 1; // 月份是从0开始的
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();

	const currentYear = now.getFullYear();
	const today = new Date(currentYear, now.getMonth(), now.getDate());
	const yesterday = new Date(currentYear, now.getMonth(), now.getDate() - 1);

	if (year === currentYear) {
		if (year == currentYear && month == now.getMonth() + 1 && day == now.getDate()) {
			return `今天 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
		} else if (date.getTime() >= yesterday.getTime() && date.getTime() < today.getTime()) {
			return `昨天 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
		} else {
			return `${month.toString().padStart(2, '0')}月${day.toString().padStart(2, '0')}日 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
		}
	} else {
		return `${year}年${month.toString().padStart(2, '0')}月${day.toString().padStart(2, '0')}日 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
	}
}

/**
 * 时间转换
 * timestamp 时间戳 ————2020年10月1日 23:00:00
 */
export const formatTimestamp2 = (timestamp) => {
	if (String(timestamp).length === 10) {
		timestamp = timestamp * 1000
	}
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = date.getMonth() + 1; // 月份是从0开始的
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return `${year}年${month.toString().padStart(2, '0')}月${day.toString().padStart(2, '0')}日 ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:${second.toString().padStart(2, '0')}`;
}

/**
 * 时间转换
 * timestamp 时间戳 ————2020-10-1 23:00:00
 */
export const formatTimestamp3 = (timestamp) => {
	if (String(timestamp).length === 10) {
		timestamp = timestamp * 1000
	}
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = date.getMonth() + 1; // 月份是从0开始的
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
}

/**
 * 时间转换
 * time 2020-10-1 ————2020年10月1日
 */
export const formatTimestamp4 = (time) => {
	let time1 = time.replace('-', '年').replace('-', '月') + '日'
	return time1
}


/**
 * 转换为农历日期
 * time：01-01 to 正月初一
 */
export const toChineseCanlender = (time) => {
	let arr = time.split('-')
	let m = Number(arr[0])
	console.log(m)
	let d = Number(arr[1])
	let chineseTime = ''
	let chineseNum = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
	switch (m) {
		case 1:
			chineseTime = '正月'
			break;
		case 2:
			chineseTime = '廿月'
			break
		case 11:
			chineseTime = '十一月'
			break
		case 11:
			chineseTime = '腊月'
			break
		default:
			chineseTime = chineseNum[m - 1] + '月'
			break
	}
	if (d < 10) {
		chineseTime += ('初' + chineseNum[d - 1])
	} else if (d > 10 && d < 20) {
		chineseTime += '十' + chineseNum[d - 10] + '号'
	} else {
		chineseTime += '廿十' + chineseNum[d - 20] + '号'
	}
	return chineseTime
}

/**
 * 转换为农历日期
 * time：2024-01-01 to 2024年正月初一
 */
export const toChineseCanlender2 = (time) => {
	let date = new Date(time)
	const year = date.getFullYear();
	const month = date.getMonth() + 1; // 月份是从0开始的
	const day = date.getDate();
	let timeObj = calendar.lunar2solar(year, month, day);
	let handleTime = timeObj.lYear + '年' + timeObj.IMonthCn + timeObj.IDayCn
	return handleTime
}